<?php
/**
 * Created by PhpStorm.
 * User: BlueIdeaLirui
 * Date: 2020/02/29
 * Time: 14:58
 */

namespace business\elasticsearch\services;

use business\elasticsearch\models\ProcurementModel;
use common\utils\DateTimeUtils;

class ProcurementRecommendService
{
    /**
     * 获取推荐列表
     * @param $pageIndex
     * @param $pageSize
     * @return array
     */
    public function getList($type, $pageIndex, $pageSize)
    {

        $condition = [];
        // 构建过滤条件
        $this->builderFilter($condition, $type);

        $from = ($pageIndex - 1) * $pageSize;

        $query = ProcurementModel::find()->query($condition)->orderBy(['sjPublishDate' => SORT_DESC]);
        $total = $query->count();
        $data = $query->offset($from)->limit($pageSize)->asArray()->all();

        $result = ['resultNum' => $total, 'resultSet' => null];

        if (!empty($data)) {
            foreach ($data as $dv) {
                $result['resultSet'][] = $dv['_source'];
            }
        }

        return $result;
    }

    /**
     * 构建过滤条件
     * @param array $query
     */
    protected function builderFilter(&$query = [], $type = 'all', $state = 0)
    {
        if ($type != 'all') {
            if ($type == 'zb') {
                $type = 'bidding';
            } else if ($type == 'zm') {
                $type = 'recruit';
            }
            $query['function_score']['query']['bool']['filter'][] = ProcurementModel::getSjType($type);
        }
        $query['function_score']['query']['bool']['filter'][] = ProcurementModel::getSjDue(
            DateTimeUtils::convertToGmtTime(DateTimeUtils::now()), $state);
    }
}
